<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>

<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashboard">仪表盘</a>
      <a href="/itemLists">商品管理</a>
      <a><cite>更新商品</cite></a>
    </span>
  </div>
  <div style="max-width:800px;margin-top: 30px;">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
          <input type="hidden" name="id" value="<%= item._id %>">
          <input type="text" name="itemName" value="<%= item.itemName %>" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-block">
          <input type="number" name="itemPrice" value="<%= item.itemPrice %>" required lay-verify="required" placeholder="请输入商品价格" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">商品库存</label>
        <div class="layui-input-block">
          <input type="number" name="itemStore" value="<%= item.itemStore %>" placeholder="请输入商品库存" autocomplete="off" class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">是否上下架</label>
        <div class="layui-input-block">
          <input type="checkbox" <%= item.onSale === 0 && 'checked' %> name="onSale" lay-skin="switch">
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品描述</label>
        <div class="layui-input-block">
          <textarea name="itemDesc" placeholder="请输入内容" class="layui-textarea"><%= item.itemDesc %></textarea>
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品封面</label>
        <div class="layui-input-block">
          <button type="button" class="layui-btn" id="upload">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
          <img src="<%= item.itemThumb %>" id="img" width="100" alt="">
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
          <div id="content"></div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
  </div>
  <script src="/static/layui/layui.js"></script>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
  <script>
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var layer = layui.layer;
      var upload = layui.upload;

      var itemThumb = '<%= item.itemThumb %>';
      // 商品封面图片上传
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        , url: '/upload/' //上传接口
        , field: 'file'
        , done: function (res) {
          //上传完毕回调
          console.log(res);
          itemThumb = res.data.url;
          $('#img').attr('src', itemThumb);
        }
        , error: function () {
          //请求异常回调
        }
      });
      // 初始化富文本编辑器
      var E = window.wangEditor;
      var editor = new E("#content");
      editor.create(); // editor.txt.html()
      // 设置富文本内容初始值
      editor.txt.html('<%- item.content %>');

      //监听提交
      form.on('submit(formDemo)', function (data) {
        var data = data.field;
        data.onSale = data.onSale === 'on' ? 0 : 1;
        data.itemThumb = itemThumb;
        data.content = editor.txt.html();
        // data.id = "<%= item._id %>";
        console.log(data);
        $.ajax({
          url: '/itemUpdate',
          type: 'POST',
          data: data,
          success: function (res) {
            if (res.code === 0) {
              // 新增成功
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function () {
                location.href="/itemLists"
              })
            } else {
              layer.msg(res.msg, {
                icon: 2
              })
            }
          }
        })
        return false;
      });
    })
  </script>
</body>

</html>